package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {
    /**
     * 分页查询敏感词
     *
     * @param dto 敏感词对象
     * @return
     */
    @Override
    public ResponseResult fingSensitiveByPage(SensitiveDto dto) {
        //判断参数是否为空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "找不到敏感词");
        }
        //判断分页参数是否存在问题
        dto.checkParam();

        //根据敏感词内容进行模糊查询
        //创建查询条件构建器
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        //判断敏感词是否为空
        if (StringUtils.isNotBlank(dto.getName())) {
            //不为空,根据查询的内容进行模糊查询
            wrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        //根据id升序排序
        wrapper.orderByAsc(AdSensitive::getId);
        //创建页面对象
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        //进行分页查询
        IPage<AdSensitive> page1 = page(page, wrapper);
        //最后返回结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
    }

    /**
     * 保存敏感词
     *
     * @param sensitive 敏感词对象
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive sensitive) {
        String sensitives = sensitive.getSensitives();
        //判断敏感词内容是否为空
        if (StringUtils.isBlank(sensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词不能为空!");
        }
        //判断长度是否大于10
        if (sensitives.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词长度不能超过10!");
        }
        //查询敏感词是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitives));
        //判断count是否大于0
        if (count > 0) {
            //说明该敏感词已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该敏感词已存在,请重试!");
        }
        //设置创建敏感词时间
        sensitive.setCreatedTime(new Date());
        //到这步说明敏感词满足条件,进行保存
        boolean save = this.save(sensitive);
        System.out.println("最后的结果为: " + save);
        //返回保存成功消息提醒
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词信息
     *
     * @param sensitive 敏感词对象
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive sensitive) {
        //判断当前对象是否为空
        String sensitives = sensitive.getSensitives();
        if (StringUtils.isBlank(sensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "修改后不能为空!");
        }
        //判断敏感词长度是否大于10
        if (sensitives.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词长度不能超过10!");
        }

        //判断修改后的敏感词是否已经在数据表中存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitives));
        //判断count是否大于0
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该敏感词已存在,请重新修改!");
        }
        //走到这步说明修改没有问题
        //进行修改
        boolean b = updateById(sensitive);
        System.out.println("最后的结果为: " + b);
        //返回修改成功结果
        return ResponseResult.okResult();
    }

    /**
     * 根据敏感词id删除敏感词
     *
     * @param id 敏感词id
     * @return
     */
    @Override
    public ResponseResult deleteSensitiveById(Integer id) {
        //判断id是否为null
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "删除失败!该敏感词不存在");
        }
        //查询敏感词是否存在
        AdSensitive sensitive = getById(id);
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "该敏感词不存在!无法删除!");
        }

        //到这步直接删除
        boolean b = removeById(id);
        System.out.println("删除的结果为: " + b);
        //返回删除成功结果
        return ResponseResult.okResult();
    }

    @Autowired
    SensitiveMapper sensitiveMapper;
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(sensitiveMapper.findAllSensitives());
    }


}
